<?php

/*
 * This class is used for as Model for Callout table 
 */

class Model_Callout extends Model_Base {

    protected $_name = 'tbl_subject_callout';
    protected $_primaryKey = 'call_id';

 
   public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('request' => 'tbl_subject_callout'))
                ->setIntegrityCheck(false)
                ->where('call_id = ?', $id)
                ->where('call_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get requests
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchCallout($where = "", $limit = "", $offset = "0", $order = 'call_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('call' => 'tbl_subject_callout'))
                ->setIntegrityCheck(false)
                ->join(array('subj' => 'tbl_subject'), "call.subj_id = subj.subj_id")
                ->where('call_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getCalloutCount($where) {
        $select = $this->select()
                ->from(array('call' => 'tbl_subject_callout'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
               ->join(array('subj' => 'tbl_subject'), "call.subj_id = subj.subj_id")
                ->where('call_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete requests
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteCallout($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_request) {
                $requestData = Array();
                $requestData = $this->fetchEntryById($_request);
                if (count($requestData) > 0) {
                    $data = array('call_status' => '2');
                    $this->update($data, 'call_id =' . (int) $_request);
                }
            }
        } else {
            $req_id = $prdtArray;
            $requestData = $this->fetchEntryById($req_id);
            if (count($requestData) > 0) {
                $data = array('call_status' => '2');
                $this->update($data, 'call_id =' . $req_id);
            }
        }
    }
    
     public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

}
